﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using Cau2;

namespace Cau2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        SinhVien sinhvien = new SinhVien();
        SinhVien sv = new SinhVien();
        DataRowView datarow;
        bool addnew = true;
        string kmsv;
        public MainWindow(DataRowView datarow, bool addnew)
            : this()
        {
            this.datarow = datarow;
            this.addnew = addnew;
            if (addnew == false)
            {
                kmsv = datarow["masv"].ToString();
                kmasv.Text = kmsv;
                khosv.Text = datarow["hosv"].ToString();
                ktensv.Text = datarow["tensv"].ToString();
                kmakhoa.Text = datarow["makhoa"].ToString();
                ksex.Text = datarow["sex"].ToString();
                kns.Text = datarow["ns"].ToString();
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = sinhvien.DanhSachSinhVien();
            datagrid.ItemsSource = dt.DefaultView;
            datagrid.SelectedValuePath = "masv";
        }

        private void thoat_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Bạn có muốn thoát không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {
                this.Close();
            }
        }
        private void them_Click(object sender, RoutedEventArgs e)
        {
            DataView dv = (DataView)datagrid.ItemsSource;
            DataRowView row = dv.AddNew();
            string masv = kmasv.Text;
            string hosv = khosv.Text;
            string tensv = ktensv.Text;
            string ngaysinh = kns.Text;
            string gioitinh = ksex.Text;
            string makhoa = kmakhoa.Text;
            sv.luusv(masv, hosv, tensv, ngaysinh, gioitinh, makhoa);
            row["kmasv"] = masv;
            row["khosv"] = hosv;
            row["ktensv"] = tensv;
            row["ns"] = ngaysinh;
            row["sex"] = gioitinh;
            row["mkhoa"] = makhoa;
            datagrid.Items.Refresh();
        }

        private void xoa_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Bạn có muốn xóa sinh viên này không ?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {
                string msv = datagrid.SelectedValue.ToString();
                sv.XoaSinhVien(msv);
                DataRowView rowSelect = (DataRowView)datagrid.SelectedItem;
                rowSelect.Delete();
            }
        }
    }
}
